package cn.com.huzo.service.purchase.impl;

import cn.com.huzo.model.entities.purchase.PoLinesSurchargesEntity;
import huzofw.service.impl.GenericManagerImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.com.huzo.service.purchase.PoLinesManager;
import cn.com.huzo.service.purchase.PoLinesSurchargesManager;
import cn.com.huzo.repository.purchase.PoLinesSurchargesRepository;

/**
 * 订单附加费管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("PoLinesSurchargesManager")
public class PoLinesSurchargesManagerImpl extends GenericManagerImpl<PoLinesSurchargesEntity, String>
        implements PoLinesSurchargesManager {

    @Resource(name = "PoLinesSurchargesRepository")
    private PoLinesSurchargesRepository _dao_lines_surcharges;

    @Resource(name = "PoLinesManager")
    private PoLinesManager _manger_lines;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public List<PoLinesSurchargesEntity> fetchAllByHeaderId(Long headerId) throws Exception {
        return _dao_lines_surcharges.fetchAllByHeaderKey(headerId);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> insert(Long headerId, Integer limit, Integer pageIndex,
            List<PoLinesSurchargesEntity> itemList) throws Exception {
        Map<String, Object> _result = new HashMap();

        Integer rowsAffected = _dao_lines_surcharges.batchInsert(itemList);
        if (rowsAffected > 0) {
            // 审计日志
            writeAuditLog(itemList.get(0).getUpdatedBy(), "采购管理→采购订单附加费", "订单号：" + itemList.get(0).getPoNo(), "增加");
        }

        _result.put("SurchargesLinesList", _dao_lines_surcharges.fetchAllByHeaderKey(headerId));
        Map<String, Object> criterion = new HashMap();
        criterion.put("HeaderId", headerId);
        criterion.put("Limit", limit);
        criterion.put("PageIndex", pageIndex);
        _result.put("PoLinesList", _manger_lines.fetchForPage(criterion));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> update(Long headerId, Integer limit, Integer pageIndex,
            List<PoLinesSurchargesEntity> itemList) throws Exception {
        Map<String, Object> _result = new HashMap();

        Integer rowsAffected = _dao_lines_surcharges.batchUpdate(itemList);
        if (rowsAffected > 0) {
            // 审计日志
            writeAuditLog(itemList.get(0).getUpdatedBy(), "采购管理→采购订单附加费", "订单号：" + itemList.get(0).getPoNo(), "更新");
        }

        _result.put("SurchargesLinesList", _dao_lines_surcharges.fetchAllByHeaderKey(headerId));
        Map<String, Object> criterion = new HashMap();
        criterion.put("HeaderId", headerId);
        criterion.put("Limit", limit);
        criterion.put("PageIndex", pageIndex);
        _result.put("PoLinesList", _manger_lines.fetchForPage(criterion));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Long headerId, String linesId, Long deletedBy,
            String poNo, String desctiption, Integer limit, Integer pageIndex) throws Exception {
        Map<String, Object> _result = new HashMap();

        Integer rowsAffected = _dao_lines_surcharges.delete(linesId);
        if (rowsAffected > 0) {
            // 审计日志
            writeAuditLog(deletedBy, "采购管理→采购订单附加费", "订单号：" + poNo, "删除");
        }

        _result.put("SurchargesLinesList", _dao_lines_surcharges.fetchAllByHeaderKey(headerId));
        Map<String, Object> criterion = new HashMap();
        criterion.put("HeaderId", headerId);
        criterion.put("Limit", limit);
        criterion.put("PageIndex", pageIndex);
        _result.put("PoLinesList", _manger_lines.fetchForPage(criterion));

        return _result;
    }
}
